package com.cc.networkmonitor;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;

import androidx.annotation.NonNull;

/**
 * 网络状态监听
 */
public class NetStateMonitor {
    private Object networkCallBack;
    private Context context;
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager conn =
                    (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = conn.getActiveNetworkInfo();
            if (networkCallBack instanceof CompatNetWorkCallback) {
                if (networkInfo == null) {
                    ((CompatNetWorkCallback) networkCallBack).onChanged(false, false);
                } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    ((CompatNetWorkCallback) networkCallBack).onChanged(true, true);
                } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    ((CompatNetWorkCallback) networkCallBack).onChanged(true, false);
                }
            }
        }
    };

    public void start(Context context, ConnectivityManager.NetworkCallback networkCallback) {
        this.context=context;
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//API 大于26时
            this.networkCallBack = networkCallback;
            connectivityManager.registerDefaultNetworkCallback(networkCallback);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//API 大于21时
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            NetworkRequest request = builder.build();
            this.networkCallBack = networkCallback;
            connectivityManager.registerNetworkCallback(request, networkCallback);
        }
    }

    public void startAuto(Activity activity, ConnectivityManager.NetworkCallback networkCallback) {
        this.context=activity;
        start(activity, networkCallback);
        activity.getApplication().registerActivityLifecycleCallbacks(new SimpleLifeCycleCallBack() {
            @Override
            public void onActivityDestroyed(@NonNull Activity act) {
                if (act == activity) {
                    act.getApplication().unregisterActivityLifecycleCallbacks(this);
                    ConnectivityManager connectivityManager =
                            (ConnectivityManager) activity.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//API 大于26时
                        connectivityManager.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkCallBack);
                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//API 大于21时
                        connectivityManager.unregisterNetworkCallback((ConnectivityManager.NetworkCallback) networkCallBack);
                    }
                }
            }
        });
    }

    public void startCompat(Context context, CompatNetWorkCallback compatNetWorkCallback) {
        this.context=context;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.getApplicationContext().registerReceiver(receiver, intentFilter);
    }

    public void unregisterCompat() {
        if (context != null) {
            context.unregisterReceiver(receiver);
        }
    }

    public void startAutoCompat(Activity activity, CompatNetWorkCallback networkCallback) {
        this.context=activity;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        activity.getApplicationContext().registerReceiver(receiver, intentFilter);
        activity.getApplication().registerActivityLifecycleCallbacks(new SimpleLifeCycleCallBack() {
            @Override
            public void onActivityDestroyed(@NonNull Activity act) {
                if (act == activity) {
                    act.getApplication().unregisterActivityLifecycleCallbacks(this);
                    act.unregisterReceiver(receiver);
                }
            }
        });
    }

    public interface CompatNetWorkCallback {
        void onChanged(boolean isEnable, boolean isWifi);
    }
}
